package com.gxkj.projects.myshopx.controllers.admin;

import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.dto.ReturnData;
import com.gxkj.projects.myshopx.entitys.GoodType;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.services.GoodsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.gxkj.projects.myshopx.util.SessionUserUtil.getUser;

@Controller
@RequestMapping("/admin/goodstype")
public class AdminGoodsTypeController {

    private  static Logger LOG = LoggerFactory.getLogger(AdminGoodsTypeController.class);

    @Autowired
    private GoodsTypeService goodsTypeService;


    @RequestMapping(value="")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
        String mv = "admin/admin_goodstype";
        return mv;
    }

    @RequestMapping(value="dopage",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<ListPager<GoodType>> dopage(GoodType entity,
                                                  int pageno, int pagesize,
                                                  HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<ListPager<GoodType>> returnData = new ReturnData<ListPager<GoodType>>();
        ListPager<GoodType> pager =  goodsTypeService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;

    }
    @RequestMapping(value="doadd",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<GoodType>doAdd(GoodType entity,
                                      HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<GoodType> returnData = new ReturnData<GoodType>();
        goodsTypeService.doAddGoodType(getUser(request),entity);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="get",method= RequestMethod.GET)
    @ResponseBody
    public  ReturnData<GoodType>get(String id,
                                    HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<GoodType> returnData = new ReturnData<GoodType>();
        GoodType entity = goodsTypeService.getById(id);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="doupdate",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<GoodType>doupdate(GoodType entity ,
                                         HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<GoodType> returnData = new ReturnData<GoodType>();
        goodsTypeService.doUpdateGoodType(getUser(request),entity);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
    @RequestMapping(value="dodel",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>dodel(String id,
                                     HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        goodsTypeService.doDeleteGoodType(getUser(request),id);
        returnData.setEntity(true);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="enabled",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>enabled(String id,
                                     HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        goodsTypeService.doEnabledGoodType(getUser(request),id);
        returnData.setEntity(true);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="disabled",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>disabled(String id,
                                       HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        goodsTypeService.doDisEnabledGoodType(getUser(request),id);
        returnData.setEntity(true);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
}
